using IQIGame.Onigao.Game;
using IQIGame.Onigao.Logic;

namespace IQIGame.Onigao.GamePlay
{
    public class CFSMState_Level_Logic_Monster_Chase : CFSMState<IQIGame.Onigao.Logic.LevelEntity, CFSMLevel_Logic_MonsterData>
    {
        private LgChaseEnemyComponent _chaseCom;
        private float _activeChaseDelay;

        public override void OnInit()
        {

        }

        public override void OnEnter(int prevStateId)
        {
            //移除巡逻组件，停止巡逻逻辑，改为追击逻辑，退出巡逻状态时只会暂停巡逻组件，仅此状态会彻底停止巡逻组件
            //因为追击状态和巡逻状态都会执行寻路逻辑，有一定冲突
            entity.RemoveComponent<LgEntityPatrolComponent>();
            _chaseCom = entity.AddComponent<LgChaseEnemyComponent>();
            data.isOutOfPost = true;
            data.chaseStartPos = entity.position;
        }

        public override void OnUpdate(float deltaTime)
        {
            _activeChaseDelay += deltaTime;
            if (_activeChaseDelay >= LgLevelConst.MonsterWarnDelay)
            {
                _chaseCom.LockEntity(data.lockingEntity);
                _activeChaseDelay = 0;
            }
        }

        public override void OnExit()
        {
            _activeChaseDelay = 0;
            _chaseCom = null;
            entity.RemoveComponent<LgChaseEnemyComponent>();
        }

        public override void OnReset()
        {
            base.OnReset();
        }
    }
}
